/*
 * Copyright (c) Jack魏 2024 - 2024, All Rights Reserved.
 */

package demo;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 布局管理
 *
 * @author Jack魏
 * @since 2024-06-17
 */
public class LayoutManagerDemo {
    public static void main(String[] args) {
        boxLayout();
    }

    /**
     * 1、流式布局 FlowLayout（默认）
     */
    public static void flowLayout() {
        // 1.创建窗体框架
        JFrame frame = new JFrame("FlowLayout 流式布局");
        // 2.流式布局，居中，水平垂直间隔10px
        frame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        for (int i = 0; i < 100; i++) {
            frame.add(new Button(i + ""));
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 2、布置容器的边框布局 BorderLayout
     */
    public static void borderLayout() {
        // 1.创建窗体框架
        JFrame frame = new JFrame("布置容器的边框布局");
        // 2.布置容器的边框布局，水平垂直间隔20px
        frame.setLayout(new BorderLayout(20, 20));
        frame.add(new Button("上北"), BorderLayout.NORTH);
        frame.add(new Button("下南"), BorderLayout.SOUTH);
        frame.add(new Button("左西"), BorderLayout.WEST);
        frame.add(new Button("右东"), BorderLayout.EAST);

        Panel panel = new Panel();
        panel.add(new Button("你好"));
        panel.add(new Label("我在中间"));
        frame.add(panel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 3、网格布局 GridLayout
     */
    public static void gridLayout() {
        // 1.创建窗体框架
        JFrame frame = new JFrame("计算器");

        // 2.输入框，布置容器的边框布局
        Panel topPanel = new Panel();
        topPanel.add(new TextField(32));
        frame.add(topPanel, BorderLayout.NORTH);

        // 3.网格布局
        Panel buttonPanel = new Panel();
        buttonPanel.setLayout(new GridLayout(2, 5, 3, 3));
        for (int i = 0; i < 10; i++) {
            buttonPanel.add(new Button(i + ""));
        }
        frame.add(buttonPanel);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 4、网格组布局管理器 GridBagLayout
     */
    public static void gridBagLayout() {
        
    }

    /**
     * 5、卡片布局管理器 CardLayout
     */
    public static void cardLayout() {
        // 1.创建窗体框架
        JFrame frame = new JFrame("图片预览");

        // 2.图片区域
        Panel topPanel = new Panel();
        CardLayout cardLayout = new CardLayout();
        topPanel.setLayout(cardLayout);

        String[] names = {"图片1","图片2","图片3","图片4","图片5"};
        for (String name : names) {
            topPanel.add(name, new Label(name));
        }
        // 放到最上面
        frame.add(topPanel, BorderLayout.CENTER);

        // 3.按钮区域
        Panel buttonPanel = new Panel();
        Button b1 = new Button("上一张");
        Button b2 = new Button("下一张");
        Button b3 = new Button("图片3");

        // 按钮事件监听
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch (e.getActionCommand()) {
                    case "上一张": cardLayout.previous(topPanel); break;
                    case "下一张": cardLayout.next(topPanel);break;
                    case "图片3": cardLayout.show(topPanel, "图片3"); break;
                }
            }
        };
        // 添加事件
        b1.addActionListener(actionListener);
        b2.addActionListener(actionListener);
        b3.addActionListener(actionListener);

        buttonPanel.add(b1);
        buttonPanel.add(b2);
        buttonPanel.add(b3);

        frame.add(buttonPanel, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 6、盒子布局 BoxLayout
     */
    public static void boxLayout() {
        // 1.创建窗体框架
        JFrame frame = new JFrame("盒子布局");

        // 3.网格布局
//        Panel yPanel = new Panel();
//        for (int i = 0; i < 10; i++) {
//            yPanel.add(new Button(i + "")); 
//        }
//
//        // 2.盒子布局，垂直排列
//        yPanel.setLayout(new BoxLayout(yPanel, BoxLayout.Y_AXIS));
//        frame.add(yPanel);

        Box xBox = Box.createHorizontalBox();
        xBox.add(new Button("按钮1"));
        // 设置水平空白区
        xBox.add(Box.createHorizontalGlue());
        xBox.add(Box.createHorizontalStrut(20));
        xBox.add(new Button("按钮2"));

        Box yBox = Box.createVerticalBox();
        // 设置垂直空白区
        yBox.add(Box.createVerticalGlue());
        yBox.add(new Button("按钮1"));
        yBox.add(Box.createVerticalStrut(20));
        yBox.add(new Button("按钮2"));
        frame.add(xBox, BorderLayout.NORTH);
        frame.add(yBox);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
